package edu.ipfw.cs590.cheat.services.mgmt;

/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 *
 * Description:
 * This module is the Server class that bridges the gap between the service
 * classes and the EJB/Persistence classes.  It is responsible for knowing
 * hoe to call the EJB layer in order to set and retrieve data in the
 * persistence layer.
 *
 * These methods throw an AMSException with the appropriate typecode set as
 */

import cheat.assignment.AssignmentManagerRemote;
import cheat.assignment.AssignmentNotFoundException;
import cheat.assignment.InvalidAssignmentArchiveException;
import cheat.authentication.InsufficientAccessException;
import cheat.authentication.InvalidSessionException;
import cheat.authentication.NoSuchRegisteredUserException;
import cheat.authentication.SecurityManagerRemote;
import cheat.authentication.UserAlreadyRegisteredException;
import cheat.entities.AccessType;
import cheat.entities.Person;
import cheat.entities.RegisteredUser;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import edu.ipfw.cs590.cheat.services.AccessRights;
import edu.ipfw.cs590.cheat.services.AssignmentWrapper;
import edu.ipfw.cs590.cheat.services.Keyword;
import edu.ipfw.cs590.cheat.services.RegisteredPerson;
import edu.ipfw.cs590.cheat.services.UserRights;
import edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException;
import edu.ipfw.cs590.cheat.services.mgmt.faults.AMSExceptionTypes;
import java.util.ArrayList;
import org.w3c.dom.Document;
import org.web_cat._1_0.assignment_sharing.Assignment;

//import org.web_cat._1_0.assignment_sharing.Keywords;


/**
 *
 * @author rrm46582
 */
public class AssignmentManagementServer

{
private AssignmentManagerRemote assignmentManagerBean = (AssignmentManagerRemote) this.lookupAssignmentManagerBean();
private SecurityManagerRemote securityManagerBean = (SecurityManagerRemote) this.lookupSecurityManagerBean();


    /**
     * makeRegisteredPerson translates the EJB RegisteredUser class
     * to the Service layer RegisteredPerson
     *
     * @param r
     * @return RegisteredPerson
     */
    protected RegisteredPerson makeRegisteredPerson(RegisteredUser r)
    {
		RegisteredPerson p = new RegisteredPerson();
        p.setAffiliation(r.getPerson().getAffiliation());
        p.setEmail(r.getPerson().getEmail());
        p.setFirstName(r.getPerson().getFirstName());
        p.setLastName(r.getPerson().getLastName());
        p.setPhone(r.getPerson().getPhone());
        p.setUrl(r.getPerson().getUrl());
        AccessRights u = new AccessRights(r.getAccessCode().getAccessCode());
        p.setRights(u);
        p.setPassword(r.getPassword());
        p.setUserId(r.getUserId());

        return p;

    }


    /**
     * makePerson translates the EJB Person class
     * to the Service layer RegisteredPerson
     *
     * @param r
     * @return RegisteredPerson
     */
    protected RegisteredPerson makePerson(Person r) throws AMSException
    {
        if ( r == null)
        {
            System.out.println("Person is null");
            throw new AMSException(AMSExceptionTypes.InvalidParameter);
        }
		RegisteredPerson p = new RegisteredPerson();
        p.setAffiliation(r.getAffiliation());
        p.setEmail(r.getEmail());
        p.setFirstName(r.getFirstName());
        p.setLastName(r.getLastName());
        p.setPhone(r.getPhone());
        p.setUrl(r.getUrl());
        //AccessRights u = new AccessRights(r.getAccessCode().getAccessCode());
        //p.setRights(u);
        //p.setPassword(r.getPassword());
        //p.setUserId(r.getUserId());

        return p;

    }



    /**
     * makeRegisteredUser translates the Service Layer's RegisterdPerson and
     * the EJB RegisteredUser class
     *
     * @param r
     * @return RegisteredUser
     */
    protected RegisteredUser makeRegistredUser(RegisteredPerson u)
    {
        RegisteredUser r = new RegisteredUser();
        Person p = new Person();
        AccessType a = new AccessType(UserRights.toString(u.getRights().getRights()));

        p.setEmail(u.getEmail());
        p.setFirstName(u.getFirstName());
        p.setLastName(u.getLastName());
        p.setPhone(u.getPhone());
        p.setUrl(u.getUrl());
        p.setAffiliation(u.getAffiliation());

        r.setPassword(u.getPassword());
        r.setPerson(p);
        r.setAccessCode(a);
        r.setUserId(u.getUserId());

        return r;
    }



    /**
     * buildDOMList:
     * This method translates a set of AssignmentWrapper classes into a
     * list of DOM objects  This will facilitate modeling AssignmentMetaData
     * as an xml file
     *
     * @param aWrappers
     * @return List<Document>
     */
    protected List<Document> buildDOMList(List<AssignmentWrapper> aWrappers)
    {
        ArrayList<Document> assignments = new ArrayList<Document>();
        int count = 0;

            for(AssignmentWrapper wrapper : aWrappers)
            {
                assignments.add(wrapper.extractDOM());
                ++count;
            }
        System.out.println("\t--->Extracted " + count + " DOM Documents");
        return assignments;
    }


    /**
     * buildMetaDataList:
     * this method maps a list of AssignmentWrappers to a list of Assignments
     * Assignments are the JAXB classes generated from the assignment-sharing.xsd
     * file.
     *
     * @param aWrappers
     * @return
     */
    protected List<Assignment> buildMetaDataList(List<AssignmentWrapper> aWrappers)
    {
        ArrayList<Assignment> assignments = new ArrayList<Assignment>();
        int count = 0;

            for(AssignmentWrapper wrapper : aWrappers)
            {
                assignments.add(wrapper.extractAssignment());
                ++count;
            }
        System.out.println("\t--->Extracted " + count + " Assignments");
        return assignments;
    }

    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * SubmitAssignment Service
     * 
     * @param token
     * @param zipString
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected String doSubmitAssignment(String token, byte[] zipString) throws AMSException

    {
        AMSExceptionTypes fault = null;
        String returnValue = null;
        try
        {
//            byte[] zipString = getAttachment(context);
            if (zipString != null)
            {
                int sessionId = new Integer(token).intValue();
                int uuid = assignmentManagerBean.submitAssignment(sessionId, zipString);
                System.out.println("\t->returned uuid = " + uuid);
                returnValue = Integer.toString(uuid);
            }
        }

        catch(NumberFormatException ex)
        {
            fault = AMSExceptionTypes.InvalidParameter;
        }
        catch (InvalidAssignmentArchiveException ex)
        {
            fault = AMSExceptionTypes.InvalidAssignmentType;
        }
        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        catch (InsufficientAccessException ex)
        {
            fault = AMSExceptionTypes.InsufficientAccess;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;

        }
    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the
     * ReplaceAssignment Service
     *
     * @param token
     * @param replaceUUID
     * @param zipString
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected String doReplaceAssignment(String token, String replaceUUID, byte[] zipString) throws AMSException
    {
        String returnValue = null;
        AMSExceptionTypes fault = null;
        try
        {
            //byte[] zipString = getAttachment(context);
            if (zipString != null)
            {
                int sessionId = new Integer(token).intValue();
                int uuid = new Integer(replaceUUID).intValue();
                int returnUUID = assignmentManagerBean.replaceAssignment(sessionId, uuid, zipString);
                returnValue = Integer.toString(returnUUID);
            }
        }
        catch(NumberFormatException ex)
        {
            fault = AMSExceptionTypes.InvalidParameter;
        }
        catch (InvalidAssignmentArchiveException ex)
        {
            fault = AMSExceptionTypes.InvalidAssignmentType;
        }

        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        catch (InsufficientAccessException ex)
        {
            fault = AMSExceptionTypes.InsufficientAccess;
        }
        catch (AssignmentNotFoundException ex)
        {
            fault = AMSExceptionTypes.AssignmentNotFound;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }

    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the
     * RemoveAssignment Service
     *
     * @param token
     * @param removeUUID
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected boolean doRemoveAssignment(String token, String removeUUID)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        boolean returnValue = false;
        try
        {
            int sessionId = new Integer(token).intValue();
            int uuid = new Integer(removeUUID).intValue();
            returnValue = assignmentManagerBean.removeAssignment(sessionId, uuid);
        }
        catch(NumberFormatException ex)
        {
            fault = AMSExceptionTypes.InvalidParameter;
        }
        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        catch (InsufficientAccessException ex)
        {
            fault = AMSExceptionTypes.InsufficientAccess;
        }
        catch (AssignmentNotFoundException ex)
        {
            fault = AMSExceptionTypes.AssignmentNotFound;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }
    }

    /**
     * Orchestrates the necessary EJB methods and classes to perform the
     * GetAssignment Service
     *
     * @param token
     * @param getUUID
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected byte[] doGetAssignment(String token, String getUUID)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        byte[] returnValue = null;
        try
        {
            int sessionId = new Integer(token).intValue();
            int uuid = new Integer(getUUID).intValue();
            returnValue = assignmentManagerBean.getAssignment(sessionId, uuid);
        }
                catch(NumberFormatException ex)
        {
            fault = AMSExceptionTypes.InvalidParameter;
        }
        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        catch (AssignmentNotFoundException ex)
        {
            fault = AMSExceptionTypes.AssignmentNotFound;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }

    }



    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * Register Service
     * 
     * @param regPerson
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected boolean doRegister(RegisteredPerson regPerson)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        boolean returnValue = false;
        try
        {
            RegisteredUser regUser = makeRegistredUser(regPerson);
            returnValue = securityManagerBean.createPendingUser(regUser);
        }
        catch (UserAlreadyRegisteredException ex)
        {
            fault = AMSExceptionTypes.AlreadExists;
        }
        catch (Exception ex)
        {
           System.out.println("Caught exception!");
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }

    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * GetPendingRegistrationRequests Service
     * 
     * @param token
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected List<RegisteredPerson> doGetPendingRegistrationRequests(String token)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        List<RegisteredUser> returnValue = null;
        ArrayList<RegisteredPerson> p = new ArrayList<RegisteredPerson>();
        try
        {
            int sessionId = new Integer(token).intValue();
            returnValue = securityManagerBean.getPendingUsers(sessionId);

            for (RegisteredUser r : returnValue)
            {
                p.add(makeRegisteredPerson(r));
            }
        }
        catch (InvalidSessionException ex)
        {
            System.out.println("invalid");
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        catch (InsufficientAccessException ex)
        {
            System.out.println("access");
            fault = AMSExceptionTypes.InsufficientAccess;
        }
       catch (Exception ex)
        {
            System.out.println("unknown");
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return p;
        }
    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * GrantRegistrationRequest Service
     * 
     * @param token
     * @param uuid
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected boolean doGrantRegistrationRequest(String token, String uuid)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        boolean returnValue = false;
        try
       {
                int sessionId = new Integer(token).intValue();
                securityManagerBean.activatePendingUser(sessionId, uuid);
                returnValue = true;
       }
        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        catch (NoSuchRegisteredUserException ex)
        {
            fault = AMSExceptionTypes.NoSuchRegisteredUser;
        }
        catch (InsufficientAccessException ex)
        {
            fault = AMSExceptionTypes.InsufficientAccess;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }

    }



    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * DenyRegistrationRequest Service
     * 
     * @param token
     * @param uuid
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected boolean doDenyRegistrationRequest(String token, String uuid)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        boolean returnValue = false;
        try
        {
            int sessionId = new Integer(token).intValue();
            securityManagerBean.denyPendingUser(sessionId, uuid);
            returnValue = true;
       }
        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        catch (NoSuchRegisteredUserException ex)
        {
            fault = AMSExceptionTypes.NoSuchRegisteredUser;
        }
        catch (InsufficientAccessException ex)
        {
            fault = AMSExceptionTypes.InsufficientAccess;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }

    }



    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * ReplaceAssignment Service
     * 
     * @param userId
     * @param password
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected String doLogin(String userId, String password)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        String returnValue = null;
        try
        {

            returnValue = new Integer(securityManagerBean.login(userId, password)).toString();

        }
        catch (NoSuchRegisteredUserException ex)
        {
            fault = AMSExceptionTypes.NoSuchRegisteredUser;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }

    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * Logout Service
     * 
     * @param token
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected boolean doLogout(String token)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        boolean returnValue = false;
        try
        {
            int sessionId = new Integer(token).intValue();
            //securityManagerBean.logout does not return a value
            //assume successful if an exception is not thrown
            securityManagerBean.logout(sessionId);
            returnValue = true;
        }
        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        //catch (NoSuchRegisteredUserException ex)
        //{
        //    fault = AMSExceptionTypes.NotFound;
        //}
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }

    }




    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * GetAccessRights Service
     * 
     * @param token
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected AccessRights doGetAccessRights(String token)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        AccessRights returnValue = null;
        try
        {
            int sessionId = new Integer(token).intValue();
            AccessType access = securityManagerBean.retrieveAccessRights(sessionId);
            returnValue = new AccessRights(access.getAccessCode());
        }
        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }

    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the
     * GetPerson Service
     *
     * @param token
     * @param email
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected RegisteredPerson doGetPerson(String token, String email)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        RegisteredPerson returnValue = null;
        try
        {
            int sessionId = new Integer(token).intValue();
            returnValue = makePerson(securityManagerBean.getPerson(sessionId, email));
        }

        catch (InvalidSessionException ex)
        {
            fault = AMSExceptionTypes.InvalidSessionId;
        }
        catch (InsufficientAccessException ex)
        {
            fault = AMSExceptionTypes.InsufficientAccess;
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
            ex.printStackTrace();
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }
    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * FindAssignementByCourse Service
     * 
     * @param course
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected List<AssignmentWrapper> doFindAssignmentByCourse(String course)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        List<AssignmentWrapper> assignments = null;
        try
        {
            assignments = assignmentManagerBean.findByCourse(course);
            //assignments = buildMetaDataList(aWrappers);
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return assignments;
        }
    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the
     * FindAssignementByKeyword Service
     *
     * @param keywords
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected List<AssignmentWrapper> doFindAssignmentByKeyword(List<Keyword> keywords)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        List<AssignmentWrapper> assignments = null;
        try
        {
            ArrayList<String> params = new ArrayList<String>();
            for (Keyword k : keywords)
            {
                System.out.println("\t->>> " + k.getKeyword());
                params.add(k.getKeyword());
            }

            assignments = assignmentManagerBean.findByKeyword(params);
            //assignments = buildMetaDataList(aWrappers);
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return assignments;
        }
    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the
     * FindAssignementByLanguage Service
     *
     * @param language
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected List<AssignmentWrapper> doFindAssignmentByLanguage(String language)
            throws AMSException
    {
        List<AssignmentWrapper> assignments = null;
        AMSExceptionTypes fault = null;
        try
        {
            assignments = assignmentManagerBean.findByLanguage(language);
            //assignments = buildMetaDataList(aWrappers);
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return assignments;
        }
    }


        /**
        * Orchestrates the necessary EJB methods and classes to perform the
        * FindAssignementByAuthor Service
        *
        * @param author
        * @return
        * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
        */
        protected List<AssignmentWrapper> doFindAssignmentByAuthor(String author)
            throws AMSException
    {
        List<AssignmentWrapper> assignments = null;
        AMSExceptionTypes fault = null;
        try
        {
            assignments = assignmentManagerBean.findByAuthor(author);
            //assignments = buildMetaDataList(aWrappers);
        }
       catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return assignments;
        }
    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the
     * GetAssignmentMetadata Service
     *
     * @param assignmentUUID
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected Assignment doGetAssignmentMetadata(String assignmentUUID)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        Assignment assignment = new Assignment();
        AssignmentWrapper wrappedAssignment = null;
        try
        {
            int uuid = new Integer(assignmentUUID).intValue();
            wrappedAssignment = assignmentManagerBean.getAssignmentMeta(uuid);
            assignment = wrappedAssignment.extractAssignment();
        }
        //catch (InvalidSessionException ex)
        //{
        //    fault = AMSExceptionTypes.InvalidSessionId;
        //}
        catch (NumberFormatException ex)
        {
            fault = AMSExceptionTypes.InvalidParameter;
        }
        catch(AssignmentNotFoundException ex)
        {
            fault = AMSExceptionTypes.AssignmentNotFound;
        }
        catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return assignment;
        }

    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the
     * GetAssignmentHistory Service
     *
     * @param assignmentUUID
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected List<Assignment> doGetAssignmentHistory(String assignmentUUID)
            throws AMSException
    {
        AMSExceptionTypes fault = null;
        List<Assignment> assignments = new ArrayList<Assignment>();
        try
        {
            int uuid = new Integer(assignmentUUID).intValue();
            List<AssignmentWrapper> wrappedAssignments = assignmentManagerBean.getAssignmentHistory(uuid);
            if ( assignments != null )
                assignments = this.buildMetaDataList(wrappedAssignments);
            else
                System.out.println("wrappedAssignments is null");
        }
        //catch (InvalidSessionException ex)
        //{
        //    fault = AMSExceptionTypes.InvalidSessionId;
        //}
        catch (NumberFormatException ex)
        {
            fault = AMSExceptionTypes.InvalidParameter;
        }
        catch(AssignmentNotFoundException ex)
        {
            fault = AMSExceptionTypes.AssignmentNotFound;
        }
        catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
		}
        finally
        {
            if (fault != null)
                throw new AMSException(fault);
            else
                return assignments;
        }

    }


    /**
     * Orchestrates the necessary EJB methods and classes to perform the 
     * GetAssignmentOwner Service
     * 
     * @param UUID
     * @return
     * @throws edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException
     */
    protected String doGetAssignmentOwner(String UUID) throws AMSException {
        AMSExceptionTypes fault = null;
        String returnValue = null;
        try
        {
            int assignmentID = new Integer(UUID).intValue();
            returnValue = assignmentManagerBean.getOwner(assignmentID);
        } catch (AssignmentNotFoundException ex)
        {
            fault = AMSExceptionTypes.AssignmentNotFound;
        } catch (NumberFormatException exc) {
            fault = AMSExceptionTypes.InvalidParameter;
        } catch (Exception ex)
        {
            fault = AMSExceptionTypes.Uknown;
            ex.printStackTrace();
		} finally {
            if (fault != null)
                throw new AMSException(fault);
            else
                return returnValue;
        }
    }


    /**
     * Gets the EJB AssignmentMangerBean instance.
     * 
     * @return
     */
    private AssignmentManagerRemote lookupAssignmentManagerBean() {
        try {
            Context c = new InitialContext();
            return (AssignmentManagerRemote) c.lookup("java:comp/env/AssignmentManagerBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }


    /**
     * Gets the EJB SercurityManagerBean instance
     *
     * @return
     */
    private SecurityManagerRemote lookupSecurityManagerBean() {
        try {
            Context c = new InitialContext();
            return (SecurityManagerRemote) c.lookup("java:comp/env/SecurityManagerBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }


}
